package com.auto.learning.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.PaihangBean;
import com.auto.learning.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PaihangAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<PaihangBean> list;
	DisplayImageOptions options;
	
	public PaihangAdapter(Context context,ArrayList<PaihangBean> pList){
		this.context = context;
		this.list = pList;
		initImageLoader();
	}
	
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	ViewHolder holder;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PaihangBean bean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.paim_adapter, null);
			holder = new ViewHolder();
			holder.tv_book_num = (TextView) convertView.findViewById(R.id.tv_book_num);
			holder.id_position = (TextView) convertView.findViewById(R.id.id_position);
			holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			holder.fg_me_userpic = (CircleImageView) convertView.findViewById(R.id.fg_me_userpic);
			holder.tv_grade = (TextView) convertView.findViewById(R.id.tv_grade);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.id_position.setText(bean.getRank()+"");
		holder.tv_username.setText(bean.getUserName()+"");
		ImageLoader.getInstance().displayImage(bean.getFaceUrl(), holder.fg_me_userpic, options);
		holder.tv_book_num.setText(bean.getBookNum()+"");
		holder.tv_grade.setText(bean.getUserGrade());
		if (bean.getIsMe() == 2) {
			holder.id_position.setTextColor(context.getResources().getColor(R.color.text_isme));
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.text_isme));
			holder.tv_book_num.setTextColor(context.getResources().getColor(R.color.text_isme));
			holder.tv_grade.setTextColor(context.getResources().getColor(R.color.text_isme));
		} else {
			holder.id_position.setTextColor(context.getResources().getColor(R.color.white));
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.white));
			holder.tv_book_num.setTextColor(context.getResources().getColor(R.color.white));
			holder.tv_grade.setTextColor(context.getResources().getColor(R.color.white));
		}
		return convertView;
	
	}
	
	class ViewHolder {
		TextView id_position, tv_username, tv_book_num,tv_grade;
		CircleImageView fg_me_userpic;
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_user)
		.showImageForEmptyUri(R.drawable.me_user)
		.showImageOnFail(R.drawable.me_user).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

}
